package com.yoyo.yyadsdk.serv;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import com.yoyo.yyadsdk.activity.KeepActivity;
import com.yoyo.yyadsdk.bean.dao.AdvScreenEntity;
import com.yoyo.yyadsdk.util.StringUtils;
import com.yoyo.yyadsdk.util.TimeUtils;

import java.util.Timer;
import java.util.TimerTask;

public class AppTablePlaqueService extends Service {

    //    private Timer mTimer;
    private Context mContext;
//    private TimerTask mTimerTask;
//    private long period = TimeUtils.SECOND * 3;
//    private long timeOut = TimeUtils.MINUTE * 2;
//
//    private int count = 0;

    //    private String aty = "";
    private AdvScreenEntity adv;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
//        mTimer = new Timer();
//        mTimerTask = new TimerTask() {
//            @Override
//            public void run() {
//                if (!StringUtils.isNull(aty)) {
//                    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
//                    String activityName = am.getRunningTasks(1).get(0).topActivity.getClassName();
//                    if (!aty.contains(activityName)) {
//                        mContext.startActivity(new Intent(mContext, KeepActivity.class)
//                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                                .addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
//                                .putExtra("adv", adv).putExtra(KeepActivity.IS_APP, true));
//                        stopSelf();
//                    }
//                }
//                count++;
//                if (count > timeOut / period) {
//                    stopSelf();
//                }
//            }
//        };
//        mTimer.schedule(mTimerTask, 0, period);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (adv == null) {
            adv = intent.getParcelableExtra("adv");
//            aty = intent.getStringExtra("aty");
//            aty = adv.getActivities();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (adv != null) {
                        mContext.startActivity(new Intent(mContext, KeepActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                .addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
                                .putExtra("adv", adv).putExtra(KeepActivity.IS_APP, true));
                    }
                    stopSelf();
                }
            }, 3000);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    private void stopTimer() {
//        if (mTimer != null) {
//            mTimer.cancel();
//            mTimer = null;
//        }
//        if (mTimerTask != null) {
//            mTimerTask.cancel();
//            mTimerTask = null;
//        }
        adv = null;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
